package com.hatic.money;

import java.util.Map;

import org.jxstar.dao.DaoParam;
import org.jxstar.service.BusinessObject;

/**
 * 投资计划金额处理类：
 * 累加合同金额，如果是子计划，还必须累加父计划的合同金额；
 * 累加已支付金额，如果是子计划，还必须累加父计划的已支付金额；
 * money_contract, money_pay
 *
 * @author TonyTan
 * @version 1.0, 2011-10-29
 */
public class PlanMoneyBO extends BusinessObject {
	private static final long serialVersionUID = 1L;
	
	/**
	 * 累加投资计划中的合同金额
	 * @param planId -- 投资计划ID
	 * @param money -- 金额数
	 * @return
	 */
	public boolean addContractMoney(String planId, double money) {
		if (!addHtMoney(planId, money)) return false;
		
		String parentId = getParentId(planId);
		if (parentId == null || parentId.length() == 0) {
			_log.showDebug("not parent planid!");
			return true;
		}
		
		if (!updateParentHtMoney(parentId)) return false;
		
		return true;
	}
	
	/**
	 * 累加投资计划中的已支付金额
	 * @param planId -- 投资计划ID
	 * @param money -- 金额数
	 * @return
	 */
	public boolean addPayMoney(String planId, double money) {
		if (!addZfMoney(planId, money)) return false;
		
		String parentId = getParentId(planId);
		if (parentId == null || parentId.length() == 0) {
			_log.showDebug("not parent planid!");
			return true;
		}
		
		if (!updateParentZfMoney(parentId)) return false;
		
		return true;
	}
	
	/**
	 * 累加投资计划中的结算金额
	 * @param planId -- 投资计划ID
	 * @param money -- 金额数
	 * @return
	 */
	public boolean addCostMoney(String planId, double money) {
		if (!addJsMoney(planId, money)) return false;
		
		String parentId = getParentId(planId);
		if (parentId == null || parentId.length() == 0) {
			_log.showDebug("not parent planid!");
			return true;
		}
		
		if (!updateParentJsMoney(parentId)) return false;
		
		return true;
	}
	
	//累加投资计划中的支付金额
	private boolean addZfMoney(String planId, double money) {
		String sql = "update money_plan set money_pay = nvl(money_pay,0) + ? where plan_id = ?";
		DaoParam param = _dao.createParam(sql);
		param.addDoubleValue(Double.toString(money));
		param.addStringValue(planId);
		
		return _dao.update(param);
	}

	//累加投资计划中的合同金额
	private boolean addHtMoney(String planId, double money) {
		String sql = "update money_plan set money_contract = nvl(money_contract,0) + ? where plan_id = ?";
		DaoParam param = _dao.createParam(sql);
		param.addDoubleValue(Double.toString(money));
		param.addStringValue(planId);
		
		return _dao.update(param);
	}
	
	//更新投资计划中的结算金额
	private boolean addJsMoney(String planId, double money) {
		String sql = "update money_plan set cost_money = nvl(cost_money, 0) + ? where plan_id = ?";
		DaoParam param = _dao.createParam(sql);
		param.addDoubleValue(Double.toString(money));
		param.addStringValue(planId);
		
		return _dao.update(param);
	}
	
	//更新父计划中的支付金额
	private boolean updateParentZfMoney(String parentId) {
		String sql = "update money_plan set money_pay = " + 
					 "(select sum(nvl(money_pay,0)) from money_plan where plan_parentid = ?) " +
					 "where plan_id = ?";
		DaoParam param = _dao.createParam(sql);
		param.addStringValue(parentId);
		param.addStringValue(parentId);
		
		return _dao.update(param);
	}
	
	//更新父计划中的合同金额
	private boolean updateParentHtMoney(String parentId) {
		String sql = "update money_plan set money_contract = " + 
					 "(select sum(nvl(money_contract,0)) from money_plan where plan_parentid = ?) " +
					 "where plan_id = ?";
		DaoParam param = _dao.createParam(sql);
		param.addStringValue(parentId);
		param.addStringValue(parentId);
		
		return _dao.update(param);
	}
	
	//更新父计划中的结算金额
	private boolean updateParentJsMoney(String parentId) {
		String sql = "update money_plan set cost_money = " + 
					 "(select sum(nvl(cost_money,0)) from money_plan where plan_parentid = ?) " +
					 "where plan_id = ?";
		DaoParam param = _dao.createParam(sql);
		param.addStringValue(parentId);
		param.addStringValue(parentId);
		
		return _dao.update(param);
	}
	
	//取子计划的父计划ID
	private String getParentId(String planId) {
		String sql = "select plan_parentid from money_plan where plan_id = ?";
		DaoParam param = _dao.createParam(sql);
		param.addStringValue(planId);
		
		Map<String, String> mpData = _dao.queryMap(param);
		if (mpData.isEmpty()) return "";
		
		return mpData.get("plan_parentid");
	}
}
